#include "mainwindow.h"
#include <QNetworkInterface>
#include <QHostInfo>
#include <QThread>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(0, 0, 800, 480);

    pushButton[0] = new QPushButton();
    pushButton[1] = new QPushButton();

    pushButton[0]->setText("获取本机信息");
    pushButton[1]->setText("清空文本信息");

    /* 按钮的大小根据文本自适应，
     * 注意setSizePolicy需要在布局中使用 */
    pushButton[0]->setSizePolicy(QSizePolicy::Fixed,
                                 QSizePolicy::Fixed);
    pushButton[1]->setSizePolicy(QSizePolicy::Fixed,
                                 QSizePolicy::Fixed);

    hWidget = new QWidget();
    vWidget = new QWidget();

    hBoxLayOut = new QHBoxLayout();
    vBoxLayOut = new QVBoxLayout();

    textBrowser = new QTextBrowser();

    hBoxLayOut->addWidget(pushButton[0]);
    hBoxLayOut->addWidget(pushButton[1]);
    /* 将水平布局设置为hWidget的布局 */
    hWidget->setLayout(hBoxLayOut);

    /* 将文本浏览框和hWidget添加到垂直布局 */
    vBoxLayOut->addWidget(textBrowser);
    vBoxLayOut->addWidget(hWidget);

    vWidget->setLayout(vBoxLayOut);

    setCentralWidget(vWidget);

    timer = new QTimer();

    connect(pushButton[0], SIGNAL(clicked()),
            this, SLOT(timerstart()));
    connect(pushButton[1], SIGNAL(clicked()),
            this, SLOT(ClearHostInfo()));
    connect(timer, SIGNAL(timeout()),
            this, SLOT(timerTimeOut()));
}

MainWindow::~MainWindow()
{
}

void MainWindow::timerstart()
{
    textBrowser->clear();
    timer->start(1000);
}

void MainWindow::timerTimeOut()
{
    /* 显示本机信息 */
    showHostInfo();
    timer->stop();
}

QString MainWindow::getHostInfo()
{
    /* 通过QHostInfo的localHostName函数获取主机名称 */
    QString str = "主机名称： " + QHostInfo::localHostName() + "\n";
    /* 获取所有的网络接口，
     *QNetworkInterface类提供主机的IP地址和网络接口的列表 */
    QList<QNetworkInterface> list
            = QNetworkInterface::allInterfaces();
    /* 遍历list */
    foreach (QNetworkInterface interface, list) {
            str += "网卡设备：" + interface.name() + "\n";
            str += "MAC地址：" + interface.hardwareAddress() + "\n";

            /* QNetworkAddressEntry类存储IP地址子网掩码和广播地址 */
            QList<QNetworkAddressEntry> entryList
             = interface.addressEntries();
            /* 遍历entryList */
            foreach(QNetworkAddressEntry entry,entryList) {
                 /* 过滤IPv6地址，只留下IPv4 */
                if(entry.ip().protocol() ==
                        QAbstractSocket::IPv4Protocol) {
                    str += "IP地址： " + entry.ip().toString() + "\n";
                    str += "子网掩码: " + entry.netmask().toString() + "\n";
                    str += "广播地址： " + entry.broadcast().toString() + "\n";
                }
            }
    }
    /* 返回网络信息 */
    return str;
}

void MainWindow::showHostInfo()
{
    /* 获取本机信息后显示到textBrowser */
    textBrowser->insertPlainText(getHostInfo());
}

void MainWindow::ClearHostInfo()
{
    /* 判断textBrowser是否为空，如果不为空则清空文本 */
    if(!textBrowser->toPlainText().isEmpty()) {
        textBrowser->clear();
    }
}
